﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace datatablegenerator.DB
{
    public class DBHelper
    {
        private static SqlConnection conn = null;
        private static SqlCommand cmd = null;
        private static SqlDataReader sdr = null;

        public DBHelper()
        {
            //string connStr = "data Source=172.18.8.63;database=CIMOM;uid=developer;pwd=Mes1234567";
            //string connStr = "Data Source=8.135.117.94,6690;Initial Catalog =CIMOM_DGWZFPC;User ID=CIMOM;Pwd=CIMOM@2020";
            //string connStr = "server=192.168.43.223;uid=sa;pwd=123;database=CIMOM_XH";
            // string connStr = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "DBSql.txt");
            string connStr = ConfigurationManager.AppSettings["MSSQL"];
            conn = new SqlConnection(connStr);
        }

        public DBHelper(string connStr)
        {
            conn = new SqlConnection(connStr);
        }

        
        public SqlConnection Open()
        {
            return GetConn();
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns></returns>
        private SqlConnection GetConn()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }

        /// <summary>
        /// 关闭连接
        /// </summary>      
        public static void Close()
        {
            if (conn.State == ConnectionState.Open)
            {
                try
                {
                    conn.Close();
                }
                catch (SqlException e)
                {
                    throw e;
                }
            }
        }


        /// <summary>
        ///  执行不带参数的增删改SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">增删改SQL语句或存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string cmdText, CommandType ct)
        {
            int res;
            try
            {
                DBHelper db = new DBHelper();
                cmd = new SqlCommand(cmdText, db.GetConn());
                cmd.CommandType = ct;
                res = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return res;
        }

        /// <summary>
        ///  执行带参数的增删改SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">增删改SQL语句或存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns>int值</returns>
        public static int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
        {
            int res;
            DBHelper db = new DBHelper();
            using (cmd = new SqlCommand(cmdText, db.GetConn()))
            {
                cmd.CommandType = ct;
                cmd.Parameters.AddRange(paras);
                res = cmd.ExecuteNonQuery();
            }
            return res;
        }

        /// <summary>
        ///  执行查询SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">查询SQL语句或存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns>Table值</returns>
        public static DataTable ExecuteQuery(string cmdText, CommandType ct)
        {
            DataTable dt = new DataTable();
            DBHelper db = new DBHelper();
            cmd = new SqlCommand(cmdText, db.GetConn());
            cmd.CommandType = ct;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }

        /// <summary>
        ///  执行带参数的查询SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">查询SQL语句或存储过程</param>
        /// <param name="paras">参数集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns>Table值</returns>
        public static DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
        {
            DataTable dt = new DataTable();
            DBHelper db = new DBHelper();
            cmd = new SqlCommand(cmdText, db.GetConn());
            cmd.CommandType = ct;
            cmd.Parameters.AddRange(paras);
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }


        /// <summary>
        /// 执行带参数的Scalar查询
        /// </summary>
        /// <param name="cmdText">查询SQL语句或存储过程</param>
        /// <param name="paras">参数集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns>一个int型值</returns>
        public int ExecuteCheck(string cmdText, SqlParameter[] paras, CommandType ct)
        {
            int result;
            using (cmd = new SqlCommand(cmdText, GetConn()))
            {
                cmd.CommandType = ct;
                cmd.Parameters.AddRange(paras);
                result = Convert.ToInt32(cmd.ExecuteScalar());
            }
            return result;
        }
    }
}
